package com.duobaoyu.dby.supply.admin.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.duobaoyu.dby.channel.adapter.enums.OperationTypeEnum;
import com.duobaoyu.dby.common.core.util.R;
import com.duobaoyu.dby.common.mybatis.util.EnumUtils;
import com.duobaoyu.dby.resource.remote.common.enums.BizTypeEnum;
import com.duobaoyu.dby.supply.admin.annotation.SupplySysLog;
import com.duobaoyu.dby.supply.biz.client.resource.ResourceTaskAsyncAdminClient;
import com.duobaoyu.dby.supply.biz.client.resource.bean.dto.TaskAsyncQueryDTO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.vo.TaskExportVO;
import com.duobaoyu.dby.supply.core.common.exception.CustomAssert;
import com.duobaoyu.dby.supply.core.task.bean.exporttask.ExcelHeader;
import com.duobaoyu.dby.supply.core.task.enums.TaskTypeEnum;
import com.duobaoyu.dby.supply.core.task.strategy.AbstractExportAsyncStrategy;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 异步导出
 * @author like
 * @version 1.0.0
 * @date 2022/6/6 9:29
 */
@Api("异步导出信息")
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/export")
public class ExportController {

	private final ResourceTaskAsyncAdminClient resourceTaskAsyncAdminClient;
	private final List<AbstractExportAsyncStrategy> strategies;

	/**
	 * 查询导出字段
	 */
	@ApiOperation(value = "查询导出字段", notes = "查询导出字段")
	@GetMapping(value = "/excel-header/{bizType}/{bizTaskType}")
	@SupplySysLog(value = "查询导出字段",operationType = OperationTypeEnum.SELECT)
	public R<List<ExcelHeader>> queryExcelHeader(@NotNull(message = "业务类型不能为空") @PathVariable("bizType") Integer bizType,
												 @NotNull(message = "任务类型不能为空") @PathVariable("bizTaskType") String bizTaskType) {
		List<ExcelHeader> excelHeaders;
		if(EnumUtils.eq(BizTypeEnum.RESOURCE,bizType)) {
			excelHeaders = resourceTaskAsyncAdminClient.queryExcelHeader(bizTaskType);
		} else {
			AbstractExportAsyncStrategy handler = strategies.stream().filter(item -> item.support(TaskTypeEnum.EXPORT_TASK.getValue(), bizTaskType)).findFirst().orElse(null);
			CustomAssert.notNull(handler, "未找到实现策略");
			excelHeaders = handler.getExcelHeader();
		}
		return R.ok(excelHeaders);
	}

	/**
	 * 查询异步导出任务(分页)
	 */
	@GetMapping(value = "/exportExcelPage")
	@ApiOperation(value = "查询异步导出任务列表", notes = "查询异步导出任务列表")
	@SupplySysLog(value = "查询异步导出任务列表",operationType = OperationTypeEnum.SELECT)
	public R<Page<TaskExportVO>> queryTaskAsyncPage(@Validated TaskAsyncQueryDTO taskAsyncQuery){
		return R.ok(resourceTaskAsyncAdminClient.queryTaskExportAsyncPage(taskAsyncQuery));
	}
}
